package com.jh.user.business.impl;

import cn.jh.common.exception.ServiceException;
import com.jh.user.business.ThirdLeveDistributionBusiness;
import com.jh.user.pojo.*;
import com.jh.user.repository.ThirdLevelDistributionRepository;
import com.jh.user.repository.ThirdLevelRateRepository;
import com.jh.user.repository.ThirdLevelRebateRatioNewRepository;
import com.jh.user.repository.ThirdLevelRebateRatioRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.*;

@Service
public class ThirdLevelDistributionBusinessImpl implements ThirdLeveDistributionBusiness{

	@Autowired
	private ThirdLevelDistributionRepository  thirdLevelDistributionRepository;

	@Autowired
	private ThirdLevelRebateRatioRepository   thirdLevelRebateRepository;

	@Autowired
	private ThirdLevelRebateRatioNewRepository thirdLevelRebateRatioNewRepository;

	@Autowired
	private EntityManager em;

	@Autowired
	private ThirdLevelRateRepository   thirdLevelRateRepository;

	@Override
	public List<ThirdLevelDistribution> getAllThirdLevelPrd(long brand) {
		List<ThirdLevelDistribution> ThirdLevelDistributions=new ArrayList<ThirdLevelDistribution>();
		ThirdLevelDistributions=thirdLevelDistributionRepository.findAllThirdLevel(brand);
		return ThirdLevelDistributions;
	}


	@Override
	public List<ThirdLevelRebateRatio> getAllThirdRatio(long brand) {

		return thirdLevelRebateRepository.findAllThirdLevelRatio(brand);
	}

	@Transactional
	@Override
	public ThirdLevelDistribution mergeThirdDistribution(
			ThirdLevelDistribution distribution) {

		ThirdLevelDistribution result =  thirdLevelDistributionRepository.save(distribution);
		em.flush();
		return result;

	}
	@Transactional
	@Override
	public void delThirdLevelByid(long id){


		thirdLevelDistributionRepository.delThirdLevelByid(id);
	}


	@Transactional
	@Override
	public ThirdLevelRebateRatio mergeThirdLevelRebateRatio(
			ThirdLevelRebateRatio rebateRatio) {

		ThirdLevelRebateRatio result =  thirdLevelRebateRepository.save(rebateRatio);
		em.flush();
		return result;

	}


	@Override
	public ThirdLevelDistribution getThirdLevelByBrandidandgrade(long brand,
																 int grade) {
		// TODO Auto-generated method stub
		return thirdLevelDistributionRepository.findAllThirdLevelByBrandidandlevelStatus(brand, grade);
	}

	@Override
	public ThirdLevelDistribution getThirdLevelByBrandidandgradeNoStatus(long brand,
																		 int grade) {
		return thirdLevelDistributionRepository.findAllThirdLevelByBrandidandlevel(brand, grade);
	}

	@Override
	public ThirdLevelRebateRatio getThirdRatioByBrandidandprelevel(long brand,
																   String prelevel) {

		return thirdLevelRebateRepository.findAllThirdLevelRatioBybrandidAndlevel(brand, prelevel);
	}


	@Override
	public ThirdLevelDistribution queryThirdLevelDistribution(long thirdlevel) {

		return thirdLevelDistributionRepository.findAllThirdLevelByid(thirdlevel);
	}

	@Override
	public List<ThirdLevelRate> findAllThirdLevelRates(long thirdlevel) {

		return thirdLevelRateRepository.findAllThirdLevelRatesBylevelid(thirdlevel);
	}

	/**
	 * 查询产品指定通道的费率
	 * **/
	@Override
	public ThirdLevelRate findAllThirdLevelRatesBylevelidAndChannelId(long thirdlevel,long channelId){
		ThirdLevelRate thirdLevelRate =new ThirdLevelRate();

		thirdLevelRate=thirdLevelRateRepository.findAllThirdLevelRatesBylevelidAndChannelId(thirdlevel, channelId);

		return thirdLevelRate;
	}

	@Override
	public List<ChannelVO> findThirdLevelChannelRate(Long brandId) {
		// TODO
		return null;
	}

	@Transactional
	@Override
	public ThirdLevelRate addThirdLevelRates(ThirdLevelRate tlr){

		ThirdLevelRate result = thirdLevelRateRepository.save(tlr);
		em.flush();
		return result;
	}


	//根据brandid查询最高等级
	@Override
	public int findThirdLevelDistributionByBrandid(long brandid) {

		return thirdLevelDistributionRepository.findThirdLevelDistributionByBrandid(brandid);
	}


	//根据grade删除
	@Transactional
	@Override
	public void deleteThirdLevelDistributionByGrade(int grade, long brandId) {
		thirdLevelRebateRatioNewRepository.deleteThirdLevelRebateByBrandIdAndGrade1(grade+"",new Long(brandId));
		thirdLevelRebateRatioNewRepository.deleteThirdLevelRebateByBrandIdAndGrade2(grade,new Long(brandId));
		thirdLevelDistributionRepository.deleteThirdLevelDistributionByGrade(grade, brandId);
	}


	@Override
	public List<ThirdLevelRebateRatioNew> getAllThirdRatio(long brandid, Integer thirdLevelId) {
		return thirdLevelRebateRatioNewRepository.findByBrandIdAndThirdLevelId(brandid,thirdLevelId);
	}


	@Override
	public ThirdLevelRebateRatioNew getByBrandIdAndPreLevelAndThirdLevelId(Long brandid, Integer thirdLevelId,
																		   String preLevel) {
		return thirdLevelRebateRatioNewRepository.findByBrandIdAndPreLevelAndThirdLevelId(brandid,preLevel,thirdLevelId);
	}

	@Transactional
	@Override
	public ThirdLevelRebateRatioNew mergeThirdLevelRebateRatio(ThirdLevelRebateRatioNew ratioModel) {
		ThirdLevelRebateRatioNew model = thirdLevelRebateRatioNewRepository.saveAndFlush(ratioModel);
		em.clear();
		return model;
	}


	@Override
	public List<ThirdLevelRebateRatioNew> getAllThirdRatioByBrandId(Long brandid) {
		return thirdLevelRebateRatioNewRepository.findByBrandId(brandid);
	}


	@Override
	public ThirdLevelRebateRatioNew getThirdRatioById(Long id) {
		return thirdLevelRebateRatioNewRepository.findById(id);
	}

	@Override
	public List<Map<String, Object>> queryThirdLevelListByBrandId(long brandId) throws ServiceException {
		List<Map<String, Object>> list = new LinkedList<>();

		List<ThirdLevelDistribution> thirdLevelDistributionList = thirdLevelDistributionRepository.queryThirdLevelListByBrandId(brandId);
		if (null == thirdLevelDistributionList || thirdLevelDistributionList.size() == 0) {
			throw new ServiceException(brandId + "下无等级列表！");
		}

		for (ThirdLevelDistribution thirdLevelDistribution : thirdLevelDistributionList) {
			Map<String, Object> map = new HashMap<>();
			String name = thirdLevelDistribution.getName();
			int grade = thirdLevelDistribution.getGrade();
			brandId = thirdLevelDistribution.getBrandId();

			map.put("name", name);
			map.put("grade", grade);
			map.put("brandId", brandId);
			list.add(map);
		}
		return list;
	}


}
